/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.search.controller;

import javax.jcr.Node;
import javax.jcr.query.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import offset.nodes.Constants;
import offset.nodes.server.controller.AuthenticatingAction;
import offset.nodes.server.dispatch.controller.DispatchAttribute;
import offset.nodes.server.search.model.SearchModel;
import offset.nodes.server.search.model.SearchType;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Walter Lütgenau
 */
public class SearchAction extends AuthenticatingAction {
    
    /** Creates a new instance of SearchAction */
    public SearchAction() {
    }
    
    public ActionForward processPersistentQuery(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // handle URL actions
        DispatchAttribute dispatchInfo = (DispatchAttribute) request.getAttribute(DispatchAttribute.ATT_DISPATCH);
        
        Node node = dispatchInfo.getNode();
        if ( node == null)
            return new ActionForward(dispatchInfo.getPath());
        
        QueryAttribute sr = new QueryAttribute(new SearchModel(getRepositorySession(request)), node.getProperty(Constants.PROP_QUERY).getString(), SearchType.xpath);
        request.setAttribute(QueryAttribute.ATT_QUERY, sr);
        
        return mapping.findForward("persistedSearch");        
     }

    public ActionForward process(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        SearchForm searchForm = (SearchForm) form;
        String search = searchForm.getQuery();
        
        if ( search == null)
            return processPersistentQuery(mapping, form, request, response);
        SearchType queryType = null;
         
        if (searchForm.getQueryType().equals(SearchForm.TYPE_FULL_TEXT))
            queryType = SearchType.text;
        else if ( searchForm.getQueryType().equals(SearchForm.TYPE_XPATH))
            queryType = SearchType.xpath;
        else if ( searchForm.getQueryType().equals(SearchForm.TYPE_SQL))
            queryType = SearchType.sql;
        
        QueryAttribute sr = new QueryAttribute(new SearchModel(getRepositorySession(request)), search, queryType);
        request.setAttribute(QueryAttribute.ATT_QUERY, sr);
        
        return mapping.findForward("search");       
    }
 }
